<template>
	<view class="bg-white">
		<titleBar title="下载专区"  text-align="center"></titleBar>
		<view>
			<uni-search-bar @confirm="search" :focus="true" v-model="searchValue" @blur="blur" @input="input"
				@cancel="cancel" @change="change" @clear="clear">
			</uni-search-bar>
			
		</view>


		<scroll-view :style="{height: scprllHeight + 'rpx'}" scroll-y="true" :refresher-enabled="isPort"
			:refresher-triggered="triggered" :refresher-threshold="100" refresher-background="lightblack"
			@refresherpulling="onPulling" @refresherrefresh="onRefresh" @refresherrestore="onRestore"
			@refresherabort="onAbort" @scrolltolower="onReachBottom"  @scroll="onPageScroll">
			<uni-list v-for="(item,index) in downloadList" :key="index">
				<uni-list-item :title="item.filename" @click="onClick(item)" show-arrow="true" link>
				</uni-list-item>
			</uni-list>
		</scroll-view>
	</view>
</template>

<script>
	import titleBar from '@/components/titleBar/titleBar.vue'
	export default {
		components: {

			titleBar,

		},
		data() {
			return {
				downloadList: [],
				scprllHeight: '',
				triggered: false,
				isPort: true,
				scrollTop: 0,
				searchValue: ''
			}
		},
		onLoad() {

			this.getDownList();


		},

		methods: {
			search(res) {
				
				this.searchValue=res.value
				this.getDownList()
				// this.searchValue=res.value,
				// this.getDownList(),
				// uni.showToast({
				// 	title: '搜索：' + res.value,
				// 	icon: 'none'
				// })
			},
			input(res) {
				console.log('----input:', res)
			},
			clear(res) {
				this.searchValue=''
				this.getDownList()
				// uni.showToast({
				// 	title: 'clear事件，输入值为：' + res.value,
				// 	icon: 'none'
				// })
			},
			blur(res) {
				// uni.showToast({
				// 	title: 'blur事件，输入值为：' + res.value,
				// 	icon: 'none'
				// })
			},
			cancel(res) {
				this.searchValue=''
				this.getDownList()
				// uni.showToast({
				// 	title: '点击取消，输入值为：' + res.value,
				// 	icon: 'none'
				// })
			},

			getDownList() {
				var that = this
				that.downloadList = []
				
				uni.request({
					url: "https://www.turkeylike.cn:8900/fzulab/download/list",
					method: 'GET',
					data: {
						
						keywords: this.searchValue
					},
					success(res) {
						

						that.downloadList = res.data.result.list


					}
				})
			},
			onClick(e) {
				console.log(e.fileurl)
				let fileUrl = encodeURI(e.fileurl); //注意中文文件名的网络地址需要encodeURI
				let that = this
				uni.downloadFile({
					url: fileUrl, //下载地址，后端接口获取的链接
					success: (data) => {
						console.log(data.tempFilePath)
						console.log(JSON.stringify(data))
						if (data.statusCode === 200) {
							uni.saveFile({ //文件保存到本地
								tempFilePath: data.tempFilePath, //临时路径
								success: function(res) {
									console.log("下载成功" + res.savedFilePath)
									console.log(JSON.stringify(res))
									uni.showToast({
										icon: 'none',
										mask: true,
										title: '文件已保存!',
										duration: 3000,
									});
									uni.openDocument({
										fileType: 'docx',
										filePath: res.savedFilePath,
										success: function(res) {
											console.log('打开文档成功');
										}
									});
								}
							});
						}
					},
					fail: (err) => {
						console.log(err);
						uni.showToast({
							icon: 'none',
							mask: true,
							title: '失败请重新下载',
						});
					},
				});


				console.log('执行click事件', e)
			},
			//是否开启自定义触发刷新
			onPageScroll(e) {
				this.scrollTop = e.scrollTop
				if (this.scrollTop < 10) {
					this.isPort = true
				} else {
					this.isPort = false
				}
			},
			// 下拉刷新操作
			//1、触发下拉操作（非刷新）
			onPulling(e) {

				this.triggered = true
			},
			//2、触发刷新
			onRefresh() {

				if (this._freshing) return
				this._freshing = true
				//显示 loading 提示框。需主动调用 wx.hideLoading 才能关闭提示框 
				wx.showLoading({
					title: '刷新中...',
				})
				// 添加定时器1秒后执行的操作
				setTimeout(() => {
					this.triggered = false
					this._freshing = false
					//重新加载数据
					this.getDownList()
				}, 1000)
			},
			//3、触发重置
			onRestore() {

				this.triggered = 'restore' // 需要重置
				wx.hideLoading();
				wx.showToast({
					title: "刷新成功",
					duration: 1000, //提示的延迟时间，单位毫秒，默认：1500 
					mask: true //是否显示透明蒙层，防止触摸穿透，默认：false 			    
				})
			},
			//4、触发终止
			onAbort() {


				//停止下拉刷新
				wx.stopPullDownRefresh();
			},
			onReachBottom() {

				wx.showToast({
					title: "已经到达底部",
					duration: 1000, //提示的延迟时间，单位毫秒，默认：1500 
					mask: false, //是否显示透明蒙层，防止触摸穿透，默认：false 			    
				})
			}
		},
	};
</script>
